<html>

<head>
	<meta charset="UTF-8">
	<title>Home</title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/style.css');?>">
	<link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
	<link href='http://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
	<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
	<script src="<?php echo base_url('js/jquery.plugin.js');?>"></script>
	<script src="<?php echo base_url('js/jquery.countdown.js');?>"></script>
	<script type="text/javascript">
		var array = <?php echo json_encode($giveaways); ?>;
 
		function resultados(){
			for (var i = 0; i < array.length; i++)
			{
		        //Criação das divs para colocar o valores obtidos
		        var give = document.createElement('div');
		        give.id = 'give';
		        document.getElementById('giveaways').appendChild(give);

		        var tipo = document.createElement('h3');
		        give.appendChild(tipo);

		        var imagem = document.createElement('div');
		        imagem.id = 'imagem';
		        give.appendChild(imagem);

		        var content = document.createElement('div');
		        content.id = 'content';
		        give.appendChild(content);

		        var titulo = document.createElement('h5');
		        content.appendChild(titulo);

		        var criado_label = document.createElement('h4');
		        content.appendChild(criado_label);

		        var criado = document.createElement('h6');
		        content.appendChild(criado);

		        var data_label = document.createElement('h4');
		        content.appendChild(data_label);

		        var data = document.createElement('h6');
		        content.appendChild(data);

		        var entradas_label = document.createElement('h4');
		        content.appendChild(entradas_label);

		        var entradas = document.createElement('h6');
		        entradas.id = 'entradas' + i;
		        content.appendChild(entradas);

		        var descricao_label = document.createElement('h4');
		        content.appendChild(descricao_label);

		        var descricao = document.createElement('h6');
		        descricao.id = 'descricao' + i;
		        content.appendChild(descricao);

		        var acaba_label = document.createElement('h4');
		        acaba_label.id = 'acaba';
		        content.appendChild(acaba_label);

		        var time = document.createElement('h6');
		        time.id = 'time' + i;
		        content.appendChild(time);

		        var entrar = document.createElement('div');
		        entrar.id = 'entrar';
		        give.appendChild(entrar);

		        var aderido = document.createElement('div');
		        aderido.id = 'aderido' + i;
		        entrar.appendChild(aderido);

		        //Obter os valores
		        //Tipo
		        if(array[i].G_TIPO){
		            tipo.innerHTML = array[i].G_TIPO;
		        }
		        else
		            tipo.innerHTML = '';
		      	//Imagem
		      	if(array[i].G_IMAGEM){
		            imagem.innerHTML = '<img src="<?php echo base_url()?>/imagens/' + array[i].G_IMAGEM + '"/>';
		        }
		        else
		            imagem.innerHTML = '';
		        //Titulo
		        if(array[i].G_TITULO){
		            titulo.innerHTML = array[i].G_TITULO;
		        }
		        else
		            titulo.innerHTML = '';
		        //Criado por:
		        criado_label.innerHTML = 'Criado por: ';
		        if(array[i].G_AUTOR){
		            criado.innerHTML = array[i].G_AUTOR;
		        }
		        else
		            criado.innerHTML = '';
		        //Data início:
		        data_label.innerHTML = 'Data início: ';
		        if(array[i].G_DATA_I){
		            data.innerHTML = array[i].G_DATA_I;
		        }
		        else
		            data.innerHTML = '';
		        //Entradas:
		        entradas_label.innerHTML = 'Entradas: ';
		        entradas.innerHTML = '0';
		        listarEntradas(array[i].G_ID);
		        //Descrição:
		        descricao_label.innerHTML = 'Descrição: ';
		        if(array[i].G_DESCRICAO){
		            descricao.innerHTML = array[i].G_DESCRICAO;
		        }
		        else
		            descricao.innerHTML = '';
		        //Acabe em::
		        acaba_label.innerHTML = 'Acaba em: ';
		       	//Botão Entrar
		        aderido.innerHTML = '<input type="submit" name="entrarbtn" id="entrarbtn' + i + '" value="Entrar" onClick="login()">';
		    }
		    countdown();
		    verMais();
		    verifica_entradas_user();
		}

		function countdown() {
			for(var j=0; j < array.length; j++)
			{
				$(function () {
					var austDay = new Date();
					austDay = new Date(array[j].G_DATA_F);
					var id = array[j].G_ID;
					var x = j;
					$('#time' + j).countdown({until: austDay, layout: '{dn}d {hn}h {mn}m {sn}s', 
						onExpiry: function() {document.getElementById("time" + x).innerHTML = "Terminado";
                    		$.ajax({
							  type: "POST",
							  url: "<?php echo base_url('/giveaways/winner');?>",
							  data: { "id": id },
							  success: function() {

							  }
							});
            			}
            		});
				});
			}
		}

		function verMais() {
			var showChar = 200;
			var ellipsestext = "...";
			var moretext = "mais";
			for(var j=0; j < array.length; j++)
			{
				$('#descricao' + j).each(function() {
					var content = $(this).html();

					if(content.length > showChar) {

						var c = content.substr(0, showChar);
						var h = content.substr(showChar-1, content.length - showChar);

						var html = c + ellipsestext + '<a href="" class="morelink">' + moretext + '</a>';

						$(this).html(html);
					}
				});
			}
		}

		function listarEntradas(id)
		{
			$.ajax({
			  type: "POST",
			  url: "<?php echo base_url('/giveaways/pesquisaEntradas');?>",
			  data: { "id": id },
			  success: function(msg) {
			  	for(var j=0; j < array.length; j++)
				{
					if(array[j].G_ID == id)
			    		document.getElementById("entradas" + j).innerHTML = msg;
			    	else{}
			    }
			  }
			});
		}

		function login()
		{
			alert("Efectue login para participar no giveaway.");
		}
	</script>
</head>


<body onload="	resultados()">

	<div class="header">
		<div id="wrapper">
			<div class="logo">
				<h1><a href="<?php echo base_url('/giveaways');?>">GIVEAWAYS</a></h1>
			</div>
			<div class="menu">
				<?php 					
					$this->load->helper("form");

					echo form_open('/giveaways/reg_or_log');

					$data = array (
						"name" => "mail",
						"id" => "mail",
						"value" => "",
						"type" => "email",
						"placeholder" => "E-Mail",
						"onClick" => "if(this.placeholder=='E-Mail')this.placeholder=''",
						"onBlur" => "if(this.placeholder=='')this.placeholder='E-Mail'"
					);
					echo form_input($data);

					$data = array (
						"name" => "pass",
						"id" => "pass",
						"value" => "",
						"type" => "password",
						"placeholder" => "Password",
						"onClick" => "if(this.placeholder=='Password')this.placeholder=''",
						"onBlur" => "if(this.placeholder=='')this.placeholder='Password'"
					);
					echo form_input($data);

					echo form_submit("loginbtn", "Login");
					echo form_submit("registarbtn", "Registar");
					echo validation_errors();

					echo form_close(); 

				?>
			</div>
		</div>
	</div>

	<div class="body">
		<div class="middle">
			<div id="wrapper">
				<div id="form_pesq">
					<form method="post" action="<?php echo base_url('/giveaways/pesquisa');?>">
				        <input type="text" name="search" id="search" value="<?= $search; ?>" placeholder="Pesquisa Giveaways" onClick="if(this.placeholder=='Pesquisa Giveaways')this.placeholder=''" onBlur="if(this.placeholder=='')this.placeholder='Pesquisa Giveaways'">
						<label class="label_check" for="sample_check">
						    <input name="sample_check[]" id="sample_check1" value="Livro" type="checkbox" <?php if(isset($sample_check)){if(in_array("Livro", $sample_check)) echo "checked='checked'";} ?> />
						    Livro
						</label>
						<label class="label_check" for="sample_check">
						    <input name="sample_check[]" id="sample_check2" value="Música" type="checkbox" <?php if(isset($sample_check)){if(in_array("Música", $sample_check)) echo "checked='checked'";} ?> />
						    Música
						</label>
						<label class="label_check" for="sample_check">
						    <input name="sample_check[]" id="sample_check3" value="Filme" type="checkbox" <?php if(isset($sample_check)){if(in_array("Filme", $sample_check)) echo "checked='checked'";} ?> />
						    Filme
						</label>
				        <input type="submit" name="searchbtn" id="searchbtn" value="Pesquisar">
				    </form>
				</div>

				<div id="giveaways">
				</div>

			</div>
		</div>
	</div>

</body>
<script type="text/javascript"> 
	alert ('<?php echo $feedback?>');
</script>

</html>
